package mailmessages;

import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.FileOutputStream;

public class TextMessage {
    public static MimeMessage generate() throws Exception {
        String from = "428904315@qq.com "; // 发件人地址
        String to = "2152343087@qq.com"; // 收件人地址

        String subject = "警告警告！！！！";
        String body = "您好,目前检测到你有非法行为，请注意你的上网行为！！！！详细请查看：+\"www.cpd.com.cn";

        // 创建Session实例对象
        Session session = Session.getDefaultInstance(new Properties());
        // 创建MimeMessage实例对象
        MimeMessage message = new MimeMessage(session);
        // 设置发件人
        String nick="";
        try {
            nick=javax.mail.internet.MimeUtility.encodeText("中国网警");
        } catch (Exception e) {
            e.printStackTrace();
        }
        message.setFrom(new InternetAddress(nick+" <"+from+">"));
        // 设置收件人
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        // 设置发送日期
        message.setSentDate(new Date());
        // 设置邮件主题
        message.setSubject(subject);
        // 设置纯文本内容的邮件正文
        message.setText(body);
        // 保存并生成最终的邮件内容
        message.saveChanges();

        // 把MimeMessage对象中的内容写入到文件中
        //msg.writeTo(new FileOutputStream("e:/test.eml"));
        return message;
    }
}
